# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=openrc
pkgver=0.52.1
_ver=${pkgver/_git*/}
pkgrel=1
pkgdesc="OpenRC manages the services, startup and shutdown of a host"
url="https://github.com/OpenRC/openrc"
arch="all"
license="BSD-2-Clause"
depends="ifupdown-any"
makedepends_host="bsd-compat-headers libcap-dev linux-headers"
makedepends_build="meson"
checkdepends="sed"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-tools
	$pkgname-static
	$pkgname-dev
	agetty-openrc:_agetty_openrc:noarch
	$pkgname-bash-completion
	$pkgname-zsh-completion
	"
install="$pkgname.post-install $pkgname.post-upgrade"
source="$pkgname-$pkgver.tar.gz::https://github.com/OpenRC/openrc/archive/$pkgver.tar.gz
	0001-call-sbin-mkmntdirs-in-localmount-OpenRC-service.patch
	0002-fsck-don-t-add-C0-to-busybox-fsck.patch
	0003-rc-pull-in-sysinit-and-boot-as-stacked-levels-when-n.patch
	0004-make-consolefont-service-compatible-with-busyboxs-se.patch
	0005-Support-early-loading-of-keymap-if-kbd-is-installed.patch
	0006-Add-support-for-starting-services-in-a-specified-VRF.patch
	0007-Clean-up-staticroute-config-remove-irrelevant-parts-.patch
	0008-bootmisc-switch-wipe_tmp-setting-to-no-by-default.patch
	0009-Based-on-the-vrf-patch-it-adds-support-to-run-servic.patch

	supervise-daemon-defaults.patch
	sysctl-readme.patch

	openrc.logrotate
	hostname.initd
	hwdrivers.initd
	modules.initd
	modloop.initd
	networking.initd
	modloop.confd
	sysfsconf.initd
	firstboot.initd
	sysctl.initd
	machine-id.initd
	test-networking.sh
	"
options="!check" # not updated to meson build system, require makefile build
provides="ifupdown-ng-openrc=0.12.1-r4"
replaces="alpine-baselayout" # due to move of mkmntdirs

# secfixes:
#   0.44.6-r1:
#     - CVE-2021-42341

prepare() {
	default_prepare
	# meson overrides this with the aports tag,
	# we get there first :)
	sed -i -e "s|@VCS_TAG@|$pkgver|" \
		src/shared/version.h.in \
		src/shared/version.in
}

build() {
	if [ -z "$BOOTSTRAP" ]; then
		local lto="-Db_lto=true"
	fi
	abuild-meson \
		$lto \
		-Dzsh-completions=true \
		-Dbash-completions=true \
		-Dpam=false \
		-Dsplit-usr=false \
		-Dpkgconfig=true \
		--default-library=both \
		--prefix=/ \
		--libdir=/usr/lib \
		--libexecdir=/lib \
		--bindir=/bin \
		--sbindir=/sbin \
		. output
	meson compile -C output
}

check() {
	make check

	# run unit tests for networking.initd
	#cd "$srcdir"
	#( set -e; sh test-networking.sh )
}

package() {
	local i j

	DESTDIR="$pkgdir" meson install --no-rebuild -C output

	# place .so's in /lib so fancy ancient separate partition /usr configs can
	# load them
	mv "$pkgdir"/usr/lib/*.so* "$pkgdir"/usr/lib/*.a \
		"$pkgdir"/lib/

	# Alpine does not use openrc as its init so delete
	# openrc-init & openrc-shutdown to avoid confusion.
	rm -f "$pkgdir"/sbin/openrc-init "$pkgdir"/sbin/openrc-shutdown

	# we cannot have anything turned on by default
	rm -f "$pkgdir"/etc/runlevels/*/*

	# we still use our ifup/ifdown based net config
	rm -f "$pkgdir"/etc/conf.d/network "$pkgdir"/etc/init.d/network

	# our hostname init script reads hostname from /etc/hostname
	rm -f "$pkgdir"/etc/conf.d/hostname

	# we override some of the scripts
	for i in "$srcdir"/*.initd; do
		j=${i##*/}
		install -Dm755 $i "$pkgdir"/etc/init.d/${j%.initd}
	done

	# we override some of the conf.d files
	for i in "$srcdir"/*.confd; do
		j=${i##*/}
		install -Dm644 $i "$pkgdir"/etc/conf.d/${j%.confd}
	done

	# additional documentation considered useful
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	install -m644 ./*.md "$pkgdir"/usr/share/doc/$pkgname/

	# we use a virtual keymaps services to allow users to set their
	# keymaps either with the OpenRC loadkeys service provided by
	# the kbd aport or with the loadkmap service provided by the
	# busybox-openrc aport.
	rm -f "$pkgdir/etc/init.d/keymaps" \
		"$pkgdir/etc/conf.d/keymaps"

	install -Dm644 "$srcdir/$pkgname.logrotate" "$pkgdir/etc/logrotate.d/$pkgname"
	install -d "$pkgdir"/etc/local.d "$pkgdir"/run

	# openrc upstream removed service(8) for whatever reason, put it back
	ln -s /sbin/rc-service "$pkgdir"/sbin/service
}

tools() {
	depends="perl"

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/share/openrc/support/deptree2dot/deptree2dot \
		"$subpkgdir"/usr/bin/
}

_agetty_openrc() {
	pkgdesc="agetty program from util-linux (OpenRC init scripts)"
	install_if="agetty openrc=$pkgver-r$pkgrel"

	amove etc/init.d/agetty
	amove etc/conf.d/agetty
}

sha512sums="
741f3f0752217b9ce159d00362d688738c5a84512ddd30c44f4f7f451d7950315a5e1de79eaf5e7b1cf6539a04f59600fbe82cc2a6ad966b735c2eb4d4f35717  openrc-0.52.1.tar.gz
257861f5f0562e9b9a9fccebae474bd75c4bb51f005a2b8132fd551f061f65863de46c5bc4437f137b4a9d3ca741f9de9274bfa7b418eda70497ed4b5fd3056d  0001-call-sbin-mkmntdirs-in-localmount-OpenRC-service.patch
3f47b4f7e6c5b7fb53ff8a13470fbada67f7470e5eba71a683e6c022162c3905f560d561c3d61698e3fde367d6ae715edf76e99949f52a22a3bbf79debc33f64  0002-fsck-don-t-add-C0-to-busybox-fsck.patch
0bd69d9e8e9c321a5e67cf924be07b9dd0b58801143c18f74bebf442958fc878e46a65f07cc2842566a8c3434e788ef3ca0c90c607de6b10931f01324bfc1b11  0003-rc-pull-in-sysinit-and-boot-as-stacked-levels-when-n.patch
71a743bf969110a27259405ef4b4dc4fad608b8e49039fd6afb1b1486d0f1dfccc3ef5275410fa3d6d1554ccee59c5a3424be4f2919e14453ebb709282c588a7  0004-make-consolefont-service-compatible-with-busyboxs-se.patch
9e2ae6c8e189ceae0f3f2662d9504f796e9a6a987a26ee2e10add85746b6596eb04cc256dc532a39f711b4e1aa07f1d12a384ef45d23cab49878b887bf0a878c  0005-Support-early-loading-of-keymap-if-kbd-is-installed.patch
8bf00b82d7fc0eb1b529ec735009f91d277141ba7e5c04e23d10bbcf36eb453f0b31d48aec45e50b5be4c14f611acc4454933f3cefdf8beab07d851328223464  0006-Add-support-for-starting-services-in-a-specified-VRF.patch
431ac28808e684bea5511386bf5f06efe7f509f1dbe7e15ae6309563d813deae8f3edd872a0943ef8088e3cf778d7bc5ebd15a893dc4a08f4022b7a56bbafc63  0007-Clean-up-staticroute-config-remove-irrelevant-parts-.patch
475f4ea63b9b5d7eb9c623e96b6cc3d3072abcb7194d0045b84e0688836c8514fccfc68b0eae0b4bee60878cdea8042c3ce7e48406ee7a2f0e4a3e128a153468  0008-bootmisc-switch-wipe_tmp-setting-to-no-by-default.patch
d7b8b1facd7fb9a6a8350912644cce3956947bcaa8bcb645f9c26a2bb0d062970efaa14bd66a7987f0748daff7b5a8e701b45b3fcca7c7c7e393ac262728f1a6  0009-Based-on-the-vrf-patch-it-adds-support-to-run-servic.patch
1323a8476580f7b56a9cf1b24b26b10da76c5916cf23d7dab01f08a3261751341dfe01d1ed884df8e6ea17ff6a52021cc40fb3101e99b77d4ae7f3f61ee330e8  supervise-daemon-defaults.patch
903ff555275a67ff08da91badb55d9fa6c6a2901e8ef09ca8366bd74d69757491c8336347f04551268ecf053c8ae974f50decd869926894dd94e382008e3e925  sysctl-readme.patch
12bb6354e808fbf47bbab963de55ee7901738b4a912659982c57ef2777fff9a670e867fcb8ec316a76b151032c92dc89a950d7d1d835ef53f753a8f3b41d2cec  openrc.logrotate
afbb4cb4f2d6c2a5cf81ee2b5da8dfcd1f573e8168f1f3586681222ccfc59d6dda65eca3f2cb97c94ce7af4da60db0c4c6c643c014358dcecf94a6152466c89c  hostname.initd
594d0b8c8a8eafdcbf6f566ec43797cbe98debfa5d9ee8b2e78344d8031184c23cf1a22588fbb4b0aaf2769458bef233332b4f8da71337db4df6a431c4bafa8f  hwdrivers.initd
bc152c2b5eb37a08035f289b21e85d41461914e8b1f3c6a6a87ba0dd0cfeb20c9a5e6d7dab132e345339245a7ae1ffcb741f5bfde76fb8e0c87e4de5e0b1faf0  modules.initd
47bea7e48444fc15f30b1da168e3d33b124c7f3480e08adcf93c5d301504cc6e73d96b254833fe20e906e353053eba8e8f410f238a3d4cd65743718a800939e5  modloop.initd
aa1dd3d31872d7ec308b534044e490136de47a3e72575a96522f60c5740ee3b953a17e05013359d27985ac16b048ff8fff76e8ac1103802a4fb75a31d2185d45  networking.initd
80e43ded522e2d48b876131c7c9997debd43f3790e0985801a8c1dd60bc6e09f625b35a127bf225eb45a65eec7808a50d1c08a5e8abceafc61726211e061e0a2  modloop.confd
d76c75c58e6f4b0801edac4e081b725ef3d50a9a8c9bbb5692bf4d0f804af7d383bf71a73d5d03ed348a89741ef0b2427eb6a7cbf5a9b9ff60a240639fa6ec88  sysfsconf.initd
990855f875513a85c2b737685ac5bfdfa86f8dadacf00c1826a456547f99b69d4ecf1b9a09c0ce002f1df618b44b1febabe53f95a2c0cd02b504d565bccb50c8  firstboot.initd
2d5f9f6d41b7c0a8643cfdee1ce3c399bfe4ebff54421f33ab1e74c1c4c1b96a49e54b5cd69f0339a060342e4e5a11067bbff68c39fa487919259d73e8e46ed1  sysctl.initd
a60129f4bed134a27773c61994d114b3a5d1ab9f20097b096956f5bb696c056f4a608577cabb34c32b51899c69dce3bd0d8ffa61f5b66fd00fc5085d1ebba54f  machine-id.initd
af17947aa3954e317dc06580da829200e0b0f2ddc37ce842c3fc7fc0d8ca2f40220e4f4665f61b4b5ec47c96416db0127e2ed979b9421bf21df89d4c4f998b7f  test-networking.sh
"
